{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "tweet_filename = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"replicable_python_tweets.json\")\n",
    "labels_filename = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"replicable_python_classes.json\")\n",
    "replicable_dataset = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"replicable_dataset.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "with open(replicable_dataset) as inf:\n",
    "    tweet_ids = json.load(inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "actual_labels = []\n",
    "label_mapping = dict(tweet_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import twitter\n",
    "consumer_key = \"59tyrGqNHCdGB92eFYEsqcjdg\"\n",
    "consumer_secret = \"DhglQERO5u936QibJP8YLbu6w60zmrxzl7jM0KmTQZZ0AXhr10\"\n",
    "access_token = \"16065520-USf3DBbQAh6ZA8CnSAi6NAUlkorXdppRXpC4cQCKk\"\n",
    "access_token_secret = \"DowMQeXqh5ZsGvZGrmUmkI0iCmI34ShFzKF3iOdiilpX5\"\n",
    "authorization = twitter.OAuth(access_token, access_token_secret, consumer_key, consumer_secret)\n",
    "t = twitter.Twitter(auth=authorization)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "all_ids = [tweet_id for tweet_id, label in tweet_ids]\n",
    "\n",
    "with open(tweet_filename, 'a') as output_file:\n",
    "    # We can lookup 100 tweets at a time, which saves time in asking twitter for them\n",
    "    for start_index in range(0, len(all_ids), 100):\n",
    "        id_string = \",\".join(str(i) for i in all_ids[start_index:start_index+100])\n",
    "        search_results = t.statuses.lookup(_id=id_string)\n",
    "        for tweet in search_results:\n",
    "            if 'text' in tweet:\n",
    "                # Valid tweet - save to file\n",
    "                output_file.write(json.dumps(tweet))\n",
    "                output_file.write(\"\\n\\n\")\n",
    "                actual_labels.append(label_mapping[tweet['id']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(labels_filename, 'w') as outf:\n",
    "    json.dump(actual_labels, outf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(260, 315)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(actual_labels), len(all_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
